/* Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com */

/*
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "label.h"
#include "../display_engine.h"

namespace tri {
namespace ui {

extLabel::extLabel() :
	tri::ui::extComponent(), mTextAlignment(ALIGN_LEFT), mFitToTextWidth(false) {
	setStyle("DefaultLabelStyle");
}

extLabel::~extLabel() {
}

void extLabel::display() {
	if (isHidden()) {
		return;
	}

	float w1, h1;
	float r_TextWidth, r_TextHeight;
	mText.getDimensions("H", w1, h1);
	mText.getDimensions(r_TextWidth, r_TextHeight);
	tri::TVector size;
	size = getSize();

	if (mFitToTextWidth) {
		size.x = r_TextWidth+getInsets().getLeft()+getInsets().getRight();
		setSize(size);
	}

	TVector v = getSize();

	tri::ui::extComponent::display();
	if (!tri::TDisplayEngine::isPicking()) {
		tri::TVector ps = getSize();

		tri::TRectangle clip_insets = getClipInsets();
		float clip[4] = { 0, 0, 0, 0 };
		tri::TRectangle insets = getInsets();
		r_TextHeight = mText.getCharacterHeight();
		float rTextYPos = ps.y/2-r_TextHeight/2;

		

		
		
		clip[1] = (r_TextHeight/2- ps.y/2) + tri::utils::max(insets.getTop() , clip_insets.getTop());
		clip[3] = (r_TextHeight/2- ps.y/2) + tri::utils::max(insets.getBottom() , clip_insets.getBottom());

		

        switch(mTextAlignment){
        case ALIGN_LEFT:
        	mText.setPosition(insets.getLeft(), rTextYPos, 1);
    		clip[2] = (insets.getLeft()+r_TextWidth) - (ps.x-tri::utils::max(insets.getRight() , clip_insets.getRight()));
			clip[0] = clip_insets.getLeft()-insets.getLeft();
			mText.setClipInsets(clip[0], clip[1], clip[2], clip[3]); 
			break;
        case ALIGN_RIGHT:
        	mText.setPosition( (ps.x - r_TextWidth ) - insets.getRight(), rTextYPos, 1);
        	clip[2] = clip_insets.getRight()-insets.getRight();
        	mText.setClipInsets(clip[0], clip[1], clip[2], clip[3]);
            break;
        case ALIGN_CENTER:
        	mText.setPosition(ps.x/2-r_TextWidth/2,rTextYPos , 1);
			clip[2] = (r_TextWidth/2- ps.x/2) + tri::utils::max(insets.getRight() , clip_insets.getRight());
			clip[0] = (r_TextWidth/2- ps.x/2) + tri::utils::max(insets.getLeft() , clip_insets.getLeft());
			mText.setClipInsets(clip[0], clip[1], clip[2], clip[3]);
           
			break;
        }
		mText.render();
	}
}

void extLabel::update() {
	extComponent::update();
	//    if(mText.repaintRequested()){
	//        requestRepaint();
	//    }
}

void extLabel::setFontSize(int s) {
	mText.setFontSize(s);
}

void extLabel::setCharacterHeight(float h) {
	mText.setCharacterHeight(h);
}

void extLabel::setFont(std::string font) {
	mText.setFont(font);
}

void extLabel::setTextBackGroundColor(int r, int g, int b, int a) {
	mText.setTextBackGroundColor(r,g,b,a);
}

void extLabel::setTextAlignment(int alignment) {
	if (alignment== ALIGN_LEFT || alignment== ALIGN_CENTER ||alignment== ALIGN_RIGHT){
		mTextAlignment = alignment;
	}
}

void extLabel::setText(std::string text){
	setText(text.c_str());
}

void extLabel::setText(const char* fmt,...){
    char text[1024]; // Holds Our String
    va_list ap; // Pointer To List Of Arguments

    if (fmt == NULL) { // If There's No Text
        return;
    } else {
        
        va_start(ap, fmt);
        // Parses The String For Variables
        vsprintf(text, fmt, ap); // And Converts Symbols To Actual Numbers
        va_end(ap);
        // Results Are Stored In Text
    }
    mText.setText(text);
}

void extLabel::setTextColor(int r, int g, int b, int a){
    mText.setTextColor(r,g,b,a);
}

void extLabel::setTextTargetColor(int r, int g, int b, int a,float t){
	mText.setTextTargetColor(r,g,b,a);
}

std::string extLabel::getText(){
    return mText.getText();
}

void extLabel::applyStyle(int state){
    extComponent::applyStyle(state);
    extStyle* style = extStyle::getStyle(getStyle());
    if(style!=NULL){
        
        tri::graphics::TColor c = style->getTextColor(mCurrentStyleState );
        setTextColor(c.getRed(),c.getGreen(),c.getBlue(),c.getAlpha());

        c = style->getTextBGColor(mCurrentStyleState );
        setTextBackGroundColor(c.getRed(),c.getGreen(),c.getBlue(),c.getAlpha());

        setFont(style->getFont(mCurrentStyleState ));
        
        setCharacterHeight(style->getCharacterHeight(mCurrentStyleState ));
        
        setFontSize(style->getFontSize(mCurrentStyleState ));
        setTextAlignment(style->getTextAlignment(mCurrentStyleState ));
        
    }
    //requestRepaint();
}

void extLabel::setFitToTextWidth(bool b){
	mFitToTextWidth=b;
}



}
}
